package com.superme.freemarkerquickstart.controller;


import com.superme.freemarkerquickstart.controller.Data.Person;
import com.superme.freemarkerquickstart.controller.Data.ResultData;
import com.superme.freemarkerquickstart.controller.Data.TableConfig;
import com.superme.freemarkerquickstart.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;

@Controller
public class PersonsController extends BaseController {

    @Autowired
    private PersonService ps;

    //重定向到querypersons.ftl
    @RequestMapping(value = "/persons/query")
    public String querypersons() {
        return "querypersons";
    }

    //重定向到querypersons.ftl
    @RequestMapping(value = "/persons/add", method = RequestMethod.GET)
    public String addperson() {
        return "addperson";
    }

    //先获取人员信息，再重定向到updateperson.ftl
    @RequestMapping(value = {"/persons/update/{personid}"}, method = RequestMethod.GET)
    public String addPerson(@PathVariable Integer personid, ModelMap map) {
        Person person = ps.findPersonById(personid);
        //将人员信息写到页面person对象中，根据ftl中的标签会自动绑定
        map.put("person", person);
        return "updateperson";
    }

    //删除人员信息，返回处理结果包
    @RequestMapping(value = {"/persons/delete/{personid}"})
    @ResponseBody
    public ResultData daletePerson(@PathVariable Integer personid) {
        return ps.deletePersonsById(personid);
    }

    //新增人员信息，返回处理结果包
    @RequestMapping(value = {"/persons/add"}, method = RequestMethod.POST)
    @ResponseBody
    public ResultData addPerson(@RequestBody Person person) {
        return ps.addPerson(person);
    }

    //更新人员信息，返回处理结果包
    @RequestMapping(value = {"/persons/update/{personid}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResultData addPerson(@PathVariable Integer personid, @RequestBody Person person) {
        return ps.updatePerson(personid, person);
    }

    //根据TABLELIST的配置信息查询人员信息，由于传入的参数不是固定的，所以只能通过HashMap map获取
    @RequestMapping(value = {"/persons/list"})
    @ResponseBody
    public HashMap getAllPerson(@RequestParam HashMap map) throws Exception {
        TableConfig tc = (TableConfig) this.mapToTableConfigObject(map, TableConfig.class);
        HashMap obj = ps.getAllPersons(tc.getSearchText(), tc.getPageNumber(), tc.getPageSize(), tc.getSortOrder(), tc.getSortName());
        //对content中各条记录的BIRTHDAY字段进行处理
        ArrayList<HashMap<String, Object>> rows = SetRowsDateFormat(obj, "birthday", "yyyy-MM-dd");
        if (rows != null)
            obj.put("content", rows);
        return obj;
    }

    /*
    @RequestMapping(value = {"/persons/add"}, method = RequestMethod.POST)
    @ResponseBody
    public HashMap addPerson(@RequestBody  HashMap map) {
        Person person = new Person();
        if (map != null) {

            if (map.containsKey("id") && map.get("id") != null) {
                int id;
                id = Integer.valueOf(map.get("id").toString()).intValue();
                person.setId(id);
            }
            if (map.containsKey("age") && map.get("age") != null) {
                Integer age;
                age = Integer.valueOf(map.get("age").toString());
                person.setAge(age);
            }
            if (map.containsKey("birthday") && map.get("birthday") != null) {
                Timestamp birthday;
                birthday = new Timestamp(Long.valueOf(map.get("birthday").toString()).longValue());
                person.setBirthday(birthday);
            }
            if (map.containsKey("height") && map.get("height") != null) {
                Integer height;
                height = Integer.valueOf(map.get("height").toString());
                person.setHeight(height);
            }
            if (map.containsKey("name") && map.get("name") != null) {
                String name;
                name = map.get("name").toString();
                person.setName(name);
            }
            if (map.containsKey("nickname") && map.get("nickname") != null) {
                String nickname;
                nickname = map.get("nickname").toString();
                person.setNickname(nickname);
            }
        }

        return ps.addPerson(person);
    }
*/

/*
    //根据TABLELIST的配置信息查询人员信息，由于传入的参数不是固定的，所以只能通过HashMap map获取
    @RequestMapping(value = {"/persons/list"})
    @ResponseBody
    public HashMap getAllPerson(@RequestParam HashMap map) throws Exception {
        //也可以用String searchText = request.getParameter("searchText");获得
        Integer pageSize = 10;
        if (map.containsKey("pageSize") && map.get("pageSize") != null)
            pageSize = Integer.valueOf(map.get("pageSize").toString());
        Integer pageNumber = 0;
        if (map.containsKey("pageNumber") && map.get("pageNumber") != null) {
            pageNumber = Integer.valueOf(map.get("pageNumber").toString());
            pageNumber = pageNumber - 1;
        }
        String searchText = "";
        if (map.containsKey("searchText") && map.get("searchText") != null)
            searchText = map.get("searchText").toString();

        String sortOrder = "";
        if (map.containsKey("sortOrder") && map.get("sortOrder") != null)
            sortOrder = map.get("sortOrder").toString();

        String sortName = "";
        if (map.containsKey("sortName") && map.get("sortName") != null)
            sortName = map.get("sortName").toString();
        HashMap obj = ps.getAllPersons(searchText, pageNumber, pageSize, sortOrder, sortName);
        //对content中各条记录的BIRTHDAY字段进行处理
        ArrayList<HashMap<String, Object>> rows = SetRowsDateFormat(obj, "birthday", "yyyy-MM-dd");
        if (rows != null)
            obj.put("content", rows);
        return obj;
    }
*/

}
